﻿using System;
using System.Collections.Generic;
using Coder.Workflow;
using Identity;

namespace Fee.Orders.PreWorkflow
{
    public class PreDelegateWorkProcess : WorkProcess<PreDelegateWorkTask>
    {
        public PreDelegateOrder CreateDelegateOrderBy(Station station, User user,
            IEnumerable<DelegateOrderDetail> operators)
        {
            if (operators == null) throw new ArgumentNullException(nameof(operators));
            var t = new PreDelegateOrder(this, user)
            {
                ConstructionTask = new ConstructionTask
                {
                    Station = station
                }
            };
            t.Add(operators);
            return t;
        }

        ///// <summary>
        /////     根据 Task 以及提交的 修改数据，制作出精确的修改系统。
        ///// </summary>
        ///// <param name="task"></param>
        ///// <param name="byOrderDetail"></param>
        ///// <returns></returns>
        //private IEnumerable<DelegateOrderDetail> MagerBy(IEnumerable<ConstructionDetail> task,
        //    IEnumerable<DelegateOrderDetail> byOrderDetail
        //)
        //{
        //    var taskDetailMap = ToMap(task);

        //    var result = new List<DelegateOrderDetail>();
        //    foreach (var orderDetail in byOrderDetail)
        //    {
        //        var key = orderDetail.ContractType + orderDetail.ConstructionType.ToString();
        //        if (taskDetailMap.ContainsKey(key))
        //        {
        //            if (orderDetail.Operator != ConstructionOperator.Cancelled)
        //            {
        //                //检查修改
        //                var detail = taskDetailMap[key];
        //                if (detail.Contract.No != orderDetail.Contract.No)
        //                {
        //                    detail.Contract = orderDetail.Contract;
        //                    orderDetail.Operator = ConstructionOperator.Changed;
        //                    orderDetail.SourceConstructionDetailId = detail.Id;
        //                    result.Add(orderDetail);
        //                }
        //                else
        //                {
        //                    //没变化
        //                    orderDetail.Operator = ConstructionOperator.Unchange;
        //                }
        //            }
        //            else
        //            {
        //                //取消
        //                orderDetail.SourceConstructionDetailId = orderDetail.Id;
        //                result.Add(orderDetail);
        //            }
        //        }
        //        else
        //        {
        //            orderDetail.Operator = ConstructionOperator.Added;
        //            result.Add(orderDetail);
        //        }
        //    }

        //    return result;
        //}


        //private Dictionary<string, ConstructionDetail> ToMap(IEnumerable<ConstructionDetail> taskDetails)
        //{
        //    var dict = new Dictionary<string, ConstructionDetail>();
        //    foreach (var item in taskDetails)
        //    {
        //        var key = item.ContractType + item.ConstructionType.ToString();
        //        if (!dict.ContainsKey(key)) dict.Add(key, item);
        //    }

        //    return dict;
        //}
    }
}